home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / tde.zip / TDEDATE.CPP < prev    next >
C/C++ Source or Header  |  1992-07-14  |  7KB  |  231 lines

  1. /***************************************************************************
  2.  
  3.  FILENAME - TDEDATE.CPP: class TDEDate definitions
  4.  ----------------------
  5.  
  6.  Class TDataEntry v1.0 - 07/14/92
  7.  --------------------------------
  8.  
  9.  ----------------------------------------------------------------------------
  10.  Author: Jeff Penrose * JDP Custom Software * (818) 344-7303 * CIS 71043,3727
  11.  ----------------------------------------------------------------------------
  12.  
  13.  A data entry class for Borland's Turbo Vision, derived from TInputLine.
  14.  
  15.  Copyright Notice
  16.  ================
  17.   As this material is ultimately derived from Borland source files, any of
  18.  their copyrights which MAY apply DO apply.
  19.   From the author's standpoint, you may use this material freely and,
  20.  hopefully, post any comments/corrections/enhancements to me at the above-
  21.  noted addresses.  I do ask that you not distribute this material except as
  22.  originally received, including all source/documentation files in their
  23.  original form.
  24.   If you DO modify or enhance any of this code, please send any such changes
  25.  to me for incorporation into a future version.  Any such enhancements will
  26.  be DONATED, without expectation of compensation or incorporation into
  27.  future versions.  Again, if you distribute this code, please do so in its
  28.  original, unmodified form including all source files and documentation.
  29.  
  30.  Source files included
  31. =====================
  32.  TDE     .DOC: This documentation.
  33.  TDE     .MAN: How to use TDataEntry in your dialog objects
  34.  TDE     .H  : header file containing class declarations for classes
  35.  TDEFLAGS.H  :   "     "      "       flags and command definitions
  36.  TDE     .CPP: Class TDataEntry
  37.  TDEDATE .CPP: Class TDEDate
  38.  TDEPHONE.CPP: Classes TDEPhone, TDEZipCode, TDEState
  39.  TDENUMS .CPP: Class TDEInteger
  40.  TDECLUST.CPP: Non-TDataEntry classes TDEButton, TDERadioButtons, TDECheckBoxes
  41.  TDEINPLI.CPP: Non-TDataEntry class TDEInputLine
  42.  TDELIB  .PRJ: Project for building library TDELIB.LIB
  43.  TDEDEMO .CPP: Demo program
  44.  TDEDEMO .PRJ: Project for building TDEDEMO.EXE
  45.  
  46. ***************************************************************************/
  47. #define   Uses_TDEDate
  48. #include  <tde.h>
  49. #include  <stdio.h>   // sprintf()
  50. #include  <stdlib.h>  // atoi(), atol()
  51. #include  <string.h>  // strlen(), strcpy()
  52.  
  53. #ifndef __LIMITS_H    // for INT_MIN, INT_MAX
  54. #include <limits.h>
  55. #endif
  56.  
  57. #ifndef ulong
  58.   typedef unsigned long  ulong;
  59. #endif
  60.  
  61. ushort TDEDate::defCentury = 19;
  62. ushort TDEDate::dateFormat = dateMDY;
  63.  
  64. //--------------------------------------------------------------------------
  65. //
  66. // **** TDEDate::getData()
  67. //
  68. //--------------------------------------------------------------------------
  69. void TDEDate::getData( void *rec )
  70. {
  71.   ulong  m, d, y, date = atol(data);
  72.   char *tmp = new char[maxLen + 1];
  73.  
  74.   if ( date > 0L && tmp != NULL )
  75.   {
  76.     strcpy(tmp, data);
  77.     switch ( dateFormat )
  78.     {
  79.       case dateDMY:
  80.         y = atol(&tmp[4]);
  81.         tmp[4] = EOS;
  82.         m = atol(&tmp[2]);
  83.         tmp[2] = EOS;
  84.         d = atol(tmp);
  85.         break;
  86.       case dateYMD:
  87.         int i = maxLen - 2;
  88.         d = atol(&tmp[i]);
  89.         tmp[i] = EOS;
  90.         i -= 2;
  91.         m = atol(&tmp[i]);
  92.         tmp[i] = EOS;
  93.         y = atol(tmp);
  94.         break;
  95.       default:  // MDY
  96.         y = atol(&tmp[4]);
  97.         tmp[4] = EOS;
  98.         d = atol(&tmp[2]);
  99.         tmp[2] = EOS;
  100.         m = atol(tmp);
  101.     }
  102.     if ( maxLen == 6 )  y += (defCentury * 100L);
  103.     date = (y * 10000L) + (m * 100L) + d;
  104.   }
  105.   else
  106.     date = 0L;
  107.  
  108.   delete tmp;
  109.  
  110.   memcpy( rec, &date, dataSize() );
  111. }
  112.  
  113. //--------------------------------------------------------------------------
  114. //
  115. // **** TDEDate::setData()
  116. //
  117. //--------------------------------------------------------------------------
  118. void TDEDate::setData( void *rec )
  119. {
  120.   ulong  date = *(ulong *)rec;
  121.  
  122.   if ( date > 0L )
  123.   {
  124.     int m = (int)((date % 10000) / 100 );
  125.     int d = (int)(date % 100 );
  126.     int y = (int)(date / 10000);
  127.     if ( maxLen == 6 )
  128.       y %= 100;
  129.  
  130.     switch ( dateFormat )
  131.     {
  132.       case dateDMY:
  133.         sprintf(data, "%02d%02d%0*d", d, m, maxLen - 4, y);
  134.         break;
  135.       case dateYMD:
  136.         sprintf(data, "%0*d%02d%02d", maxLen - 4, y, m, d);
  137.         break;
  138.       default:  // MDY
  139.         sprintf(data, "%02d%02d%0*d", m, d, maxLen - 4, y);
  140.     }
  141.   }
  142.   else
  143.     *data = EOS;
  144.  
  145.   strcpy(origData, data);
  146. }
  147.  
  148. //--------------------------------------------------------------------------
  149. //
  150. // **** TDEDate::valid()
  151. //
  152. //--------------------------------------------------------------------------
  153. Boolean TDEDate::valid(ushort cmd)
  154. {
  155.   if ( cmd == cmValid && !(maxLen == 6 || maxLen == 8) )
  156.     return False;
  157.  
  158.   return TDataEntry::valid(cmd);
  159. }
  160.  
  161. //--------------------------------------------------------------------------
  162. //
  163. // **** TDEDate::validData()
  164. //
  165. //--------------------------------------------------------------------------
  166. #define IsLeap(year) ( ( (year % 4 == 0 && year % 100 != 0) ||\
  167.                          (year % 400 == 0) ) ? 1 : 0 )
  168.  
  169. #define DaysInMonth(month, year)\
  170.   ( (month == 2) ? 28 + IsLeap(year) :\
  171.     (month == 4 || month ==  6 || month ==  9 || month == 11) ? 30 : 31 )
  172.  
  173. Boolean TDEDate::validData( const char *, const char *)
  174. {
  175.   int      m, d, y, len = strlen(data);
  176.   Boolean  dataOK = True;
  177.   char     *msg = "\003Invalid date!";
  178.  
  179.   if ( !TDataEntry::validData(msg, "\003Date is required!") )
  180.     return False;
  181.  
  182.   if ( len && !(len == 6 || len == 8) )
  183.     dataOK = False;
  184.  
  185.   if ( dataOK && len )
  186.   {
  187.     char tmp[9];
  188.     strcpy(tmp, data);
  189.     switch ( dateFormat )
  190.     {
  191.       case dateDMY:
  192.         y = atoi(&tmp[4]);
  193.         tmp[4] = EOS;
  194.         m = atoi(&tmp[2]);
  195.         tmp[2] = EOS;
  196.         d = atoi(tmp);
  197.         break;
  198.       case dateYMD:
  199.         int i = maxLen - 2;
  200.         d = atoi(&tmp[i]);
  201.         tmp[i] = EOS;
  202.         i -= 2;
  203.         m = atoi(&tmp[i]);
  204.         tmp[i] = EOS;
  205.         y = atoi(tmp);
  206.         break;
  207.       default:  // MDY
  208.         y = atoi(&tmp[4]);
  209.         tmp[4] = EOS;
  210.         d = atoi(&tmp[2]);
  211.         tmp[2] = EOS;
  212.         m = atoi(tmp);
  213.     }
  214.     if ( maxLen == 6 )
  215.       y += (defCentury * 100);
  216.     if ( m < 1 || m > 12  || d < 1 || y < 1800 ||
  217.          y > 3000 || d > DaysInMonth(m, y)
  218.        )
  219.       dataOK = False;
  220.   }
  221.  
  222.   if ( !dataOK )
  223.   {
  224.     if ( (globalMode & tdgBeepEnable) != 0 )  cout << (char) 7;
  225.     messageBox(msg, mfError | mfOKButton);
  226.     select();
  227.   }
  228.  
  229.   return dataOK;
  230. }
  231.